package com.ruoyi.order.proxy.factory;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.alibaba.fastjson2.TypeReference;
import com.ruoyi.order.proxy.factory.req.*;
import com.ruoyi.order.proxy.factory.req.print.PrintRequest;
import com.ruoyi.order.proxy.factory.req.send.SendBarcodeRequest;
import com.ruoyi.order.proxy.factory.res.*;
import com.ruoyi.order.service.IMesTopOrderService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.nio.charset.StandardCharsets;
import java.util.Collections;
import java.util.List;

@Slf4j
@Service
public class FactoryProxy {
    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private IMesTopOrderService mesTopOrderService;

    /**
     *
     * @param sendBarcodeRequest
     * @return
     */
    public boolean sendProductData2Factory(SendBarcodeRequest sendBarcodeRequest) {
        BaseRequest<SendBarcodeRequest> baseRequest = new BaseRequest<>();
        baseRequest.setData(sendBarcodeRequest);
        log.info("开始发送装配数据到工厂：{}", JSON.toJSONString(baseRequest));
        String url =    "https://esb.nrec.com:9680/esb.nrec.prod.messp.barcode";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("OperationCode", "esb.nrec.prod.messp.barcode.bmuscanbarcode");
        headers.set("ClientId", "com.primeton.esb.consumer.mes");
        HttpEntity<BaseRequest<SendBarcodeRequest>> request = new HttpEntity<>(baseRequest, headers);
        try {
            ResponseEntity<String>  response = restTemplate.postForEntity(url, request, String.class );
            String result = response.getBody();
            log.info("装配数据发送工厂返回结果：{}", result);
            JSONObject dataResult = JSON.parseObject(result);
             if (dataResult != null && Integer.valueOf(dataResult.getString("code"))==0){
                return true;
            }else {
                return false;
            }
        } catch (Exception ex) {
            log.error("发送装配数据到工厂异常：", ex);
        }
        return false;
    }


    /**
     * 条码回传
     * @param printRequest
     * @return
     */
    public ProcessDataResponse sendPrint2Factory(PrintRequest printRequest) {
        BaseRequest<PrintRequest> baseRequest = new BaseRequest<>();
        baseRequest.setData(printRequest);
        log.info("条码开始发送到工厂：{}", JSON.toJSONString(baseRequest));
        String url =    "https://esb.nrec.com:9680/esb.nrec.prod.messp.barcode";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("OperationCode", "esb.nrec.prod.messp.barcode.savemarklog");
        headers.set("ClientId", "com.primeton.esb.consumer.mes");
        HttpEntity<BaseRequest<PrintRequest>> request = new HttpEntity<>(baseRequest, headers);
        try {
            ResponseEntity<String>  response = restTemplate.postForEntity(url, request, String.class );
            String result = response.getBody();
            log.info("条码打印工厂返回结果：{}", result);
            return JSON.parseObject(result, new TypeReference<ProcessDataResponse<WorkShopResponse>>() {});
        } catch (Exception ex) {
            log.error("发送数据到工厂异常：", ex);
        }
        return null;
    }
    /**
     * 查询车间列表
     * @return
     */
    public List<WorkShopResponse> queryWorkShopList() {
        QueryWorkShopRequset queryWorkShopRequset = new QueryWorkShopRequset();
        log.info("查询车间列表：{}", JSON.toJSONString(queryWorkShopRequset));
        String url =    "https://esb.nrec.com:9680/esb.nrec.prod.messp.shopmanage";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("OperationCode", "esb.nrec.prod.messp.shopmanage.getworkshop");
        headers.set("ClientId", "com.primeton.esb.consumer.mes");
        BaseRequest<QueryWorkShopRequset> baseRequest = new BaseRequest<>();
        baseRequest.setData(queryWorkShopRequset);
        HttpEntity<BaseRequest> request = new HttpEntity<>(baseRequest, headers);
        try {
            ResponseEntity<String> response = restTemplate.postForEntity( url, request, String.class );
            String result = response.getBody();
            log.info("查询车间列表返回结果：{}", result);
            if (StringUtils.isNotEmpty(result)){
                BaseResponse<WorkShopResponse> workShopRes = JSON.parseObject(result,
                        new TypeReference<BaseResponse<WorkShopResponse>>() {});
                return workShopRes.getData();
            }
        } catch (Exception ex) {
            log.error("查询车间列表异常：", ex);
        }
        return null;
    }
    /**
     * 查询产线列表
     * @param queryLineRequest
     * @return
     */

    public List<LineResponse> queryLineList(QueryLineRequest queryLineRequest) {
        log.info("查询产线列表入参：{}", JSON.toJSONString(queryLineRequest));
        String url =    "https://esb.nrec.com:9680/esb.nrec.prod.messp.shopmanage";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("OperationCode", "esb.nrec.prod.messp.shopmanage.getline");
        headers.set("ClientId", "com.primeton.esb.consumer.mes");
        BaseRequest<QueryLineRequest> baseRequest = new BaseRequest<>();
        baseRequest.setData(queryLineRequest);
        HttpEntity<BaseRequest> request = new HttpEntity<>(baseRequest, headers);
        try {
            ResponseEntity<String> response = restTemplate.postForEntity( url,  request,String.class);
            String result = response.getBody();
            log.info("查询产线列表返回结果：{}", result);
            if (StringUtils.isNotEmpty(result)){
                BaseResponse<LineResponse> workShopRes = JSON.parseObject(result,
                        new TypeReference<BaseResponse<LineResponse>>() {});
                return workShopRes.getData();
            }
        } catch (Exception ex) {
            log.error("查询产线列表异常：", ex);
        }
        return null;
    }

    /**
     * 查询产线列表
     * @param queryBarcodeRequset
     * @return
     */

    public BarcodeResponse queryBarcodeByOrder(QueryBarcodeRequset queryBarcodeRequset) {
        log.info("根据订单查询产品条码入参：{}", JSON.toJSONString(queryBarcodeRequset));
        String url =    "https://esb.nrec.com:9680/esb.nrec.prod.messp.barcode";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("OperationCode", "esb.nrec.prod.messp.barcode.getnumbarcodeinfo");
        headers.set("ClientId", "com.primeton.esb.consumer.hanslaster");
        BaseRequest<QueryBarcodeRequset> baseRequest = new BaseRequest<>();
        baseRequest.setData(queryBarcodeRequset);
        HttpEntity<BaseRequest> request = new HttpEntity<>(baseRequest, headers);
        try {
            ResponseEntity<String> response = restTemplate.postForEntity( url,  request,String.class );
            String result = response.getBody();
            log.info("根据订单查询产品条码返回结果：{}", result);
            if (StringUtils.isNotEmpty(result)) {
                BaseResponse<BarcodeResponse> barcodeRes = JSON.parseObject(result, new TypeReference<BaseResponse<BarcodeResponse>>() {});
                if (barcodeRes != null && CollectionUtils.isNotEmpty(barcodeRes.getData())){
                    return barcodeRes.getData().get(0);
                }
            }
            log.info("查询产品条码返回结果：{}", JSON.toJSONString(response));
        } catch (Exception ex) {
            log.error("查询产线列表异常：", ex);
        }
        return null;
    }
    /**
     * 查询订单工序
     * @param querySeqRequest
     * @return
     */

    public List<OrderSeqResponse> queryOrderSeqByOrder(QuerySeqRequest querySeqRequest) {
        log.info("根据订单查询产品条码入参：{}", JSON.toJSONString(querySeqRequest));
        String url = "https://esb.nrec.com:9680/esb.nrec.mes.webprojectmes.getltxa";
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        headers.set("OperationCode", "esb.nrec.mes.webprojectmes.getltxa.select");
        headers.set("ClientId", "com.primeton.esb.consumer.mes");
        BaseRequest<QuerySeqRequest> baseRequest = new BaseRequest<>();
        baseRequest.setData(querySeqRequest);
        HttpEntity<BaseRequest> request = new HttpEntity<>(baseRequest, headers);
        try {
            ResponseEntity<String> response = restTemplate.postForEntity( url, request,String.class);
            String result = response.getBody();
            log.info("查询产品条码返回结果：{}", result);
            if (StringUtils.isNotEmpty(result)) {
                BaseResponse<OrderSeqResponse> orderSeqRes = JSON.parseObject(result, new TypeReference<BaseResponse<OrderSeqResponse>>() {});
                if (orderSeqRes != null && CollectionUtils.isNotEmpty(orderSeqRes.getData())){
                    return orderSeqRes.getData();
                }
            }
        } catch (Exception ex) {
            log.error("查询产线列表异常：", ex);
        }
        return null;
    }


}
